package org.jeecg.common.util;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.jeecg.common.dto.RealNameAuthenticationResult;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;

import lombok.extern.slf4j.Slf4j;

/**
 * @author cold
 * @data 2024/6/1 15:31
 * @version 1.0
 */

@Slf4j
public class RealNameAuthenticationUtils {
    public static final String host = "https://checkone.market.alicloudapi.com";
    public static final String path = "/chinadatapay/1882";
    public static final String method = "POST";
    public static final String appcode = "701c5561521b4435a7aea94f9567741c";

    public static Boolean authRealName(String name, String idCardNo) {
        Map<String, String> headers = Maps.newHashMap();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("name", name);
        bodys.put("idcard", idCardNo);
        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            String json = EntityUtils.toString(response.getEntity());
            log.info("实名认证结果，realName:{}, response:{}", name, JSON.toJSONString(json));
            RealNameAuthenticationResult realNameAuthenticationResult = JSONObject.parseObject(json, RealNameAuthenticationResult.class);
            if (realNameAuthenticationResult.getCode().equals("10000")) {
                Map<String, Object> resultMap = realNameAuthenticationResult.getData();
                if (resultMap.get("result").equals("1")) {
                    return true;
                }
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
